define(window.backboneCommon.getViewDefine("targets.keyword"), function ($,require,Collection,tpl) {
    var defaultsOptions={
        submitCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "keydown #txt_modal_targetKeyWord": "enterKeyword",
            "click #submit_modal_targetKeyWord": "saveSubmit"
        },
        initialize: function () {
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                keyword:$('#txt_modal_targetKeyWord'),
                modal:$('#modal_targetKeyWord')
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
        },
        initPage:function (json) {
            if(!this.pageData.initPage){
                this.pageData.initPage=true;
            }
            this.elementModel.keyword.val($.ExtCommon.isCheckEmpty(json)?"":json);
            this._showModal();
        },
        enterKeyword:function (e) {
            if(e.keyCode === 13) {
                e.preventDefault();
            }
        },
        _checkForm:function () {
            $.ExtNotify.destroy();
            var errmsg="";
            var value= $.trim(this.elementModel.keyword.val());
            var arrValue=value.replace(/，/g, ',').split(",");
            if(arrValue.length>150){
                errmsg = "关键字个数不能超过150个";
            }else {
                $.each(arrValue, function (i, info) {
                    var len =info.length;
                    if (len === 0) {
                        errmsg = "关键字不能为空";
                        return false
                    } else if (len > 10) {
                        errmsg = "每个关键字不超过10个字";
                        return false
                    }
                });
            }
            var isErr=errmsg.length===0;
            if(!isErr){
                $.ExtNotify.error(errmsg);
            }
            return isErr
        },
        saveSubmit:function () {
            if(this._checkForm()){
                var value= $.trim(this.elementModel.keyword.val());
                var arrValue=value.replace(/，/g, ',').split(",");
                this._hideModal();
                var jsonData={value:value,list:arrValue};
                if(this.pageOptions.submitCallback){this.pageOptions.submitCallback(jsonData)}
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (json) {
            this.view.initPage(json);
        }
    };
    return viewModel;
});